package com.education.wisdom.controller.management;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.education.common.config.BusinessException;
import com.education.common.config.CommonErrorCode;
import com.education.common.utils.ApiReturnObject;
import com.education.wisdom.pojo.LessonMenu;
import com.education.wisdom.service.LessonMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


/**
 * 教师私有备课目录表
 *
 * @date 2020-07-09 14:51:34
 */
@RestController
@RequestMapping("/manage/lessonMenu")
public class LessonMenuController {

    @Autowired
    private LessonMenuService lessonMenuService;

    /**
     * 排序
     * type 1 向上排序  2 向下排序
     */
    @GetMapping("/sort")
    public ApiReturnObject sort(Integer type, Integer sort) {
        if (type == 1){
            LessonMenu lessonMenu = lessonMenuService.selectBySort(sort);//查询当前
            Integer asc = lessonMenuService.selectOrderNumASC(sort);
            LessonMenu less = lessonMenuService.selectBySort(asc);//查询加一
            if (less == null){
                 return new ApiReturnObject("201", "操作失败", "已经是最高级");
            }
            LessonMenu menu = new LessonMenu();
            menu.setId(lessonMenu.getId());
            menu.setOrderNum(less.getOrderNum());
            lessonMenuService.updateLessonMenu(menu);

            LessonMenu men = new LessonMenu();
            men.setId(less.getId());
            men.setOrderNum(lessonMenu.getOrderNum());
            lessonMenuService.updateLessonMenu(men);

        }else if (type == 2){
            LessonMenu lessonMenu = lessonMenuService.selectBySort(sort);//查询当前
            Integer desc = lessonMenuService.selectOrderNumDESC(sort);
            LessonMenu less = lessonMenuService.selectBySort(desc);//查询加一
            if (less == null){
                return new ApiReturnObject("201", "操作失败", "已经是最低级");
            }
            LessonMenu menu = new LessonMenu();
            menu.setId(lessonMenu.getId());
            menu.setOrderNum(less.getOrderNum());
            lessonMenuService.updateLessonMenu(menu);

            LessonMenu men = new LessonMenu();
            men.setId(less.getId());
            men.setOrderNum(lessonMenu.getOrderNum());
            lessonMenuService.updateLessonMenu(men);
        }

        return new ApiReturnObject("200", "操作成功", "");
    }

    /**
     * 查询
     *
     * @return
     */
    @GetMapping("/search")
    public ApiReturnObject search(LessonMenu lessonMenu, Integer pageNum, Integer pageSize) {
        if (pageNum != null && pageSize != null){
            PageHelper.startPage(pageNum,pageSize);
        }
        List<LessonMenu> lessonMenuList = lessonMenuService.selectLessonMenuList(lessonMenu);
        return new ApiReturnObject("200", "操作成功", new PageInfo<LessonMenu>(lessonMenuList));
    }

//    /**
//     * 备课资源目录查询
//     */
//    @GetMapping("/selectList")
//    public ApiReturnObject selectList() {
//        List<LessonMenu> lessonMenuList = lessonMenuService.selectByParentId(0);
//        recursive(lessonMenuList);
//        return new ApiReturnObject("200", "success", lessonMenuList);
//    }
//
//    //递归查询课程
//    public void recursive(List<LessonMenu> lessonMenuList) {
//        for (LessonMenu lessonMenu : lessonMenuList) {
//            if (isNext(lessonMenu.getId())) {
//                List<LessonMenu> list1 = lessonMenuService.selectByParentId(lessonMenu.getId());
//                lessonMenu.setLessionList(list1);
//                recursive(list1);
//            }
//        }
//    }
//
//    //查询课程是否有下一级
//    public boolean isNext(Integer catalogId) {
//        List<LessonMenu> list = lessonMenuService.selectByParentId(catalogId);
//        if (list.size() == 0) {
//            return false;
//        } else {
//            return true;
//        }
//    }

    /**
     * 添加
     */
    @PostMapping("/ins")
    public ApiReturnObject insert(@RequestBody LessonMenu lessonMenu) {
        Integer integer = lessonMenuService.selectMax();
        lessonMenu.setOrderNum(integer + 1);
        lessonMenu.setCreateTime(new Date());
        int i = lessonMenuService.insertLessonMenu(lessonMenu);
        if (i > 0){
            return new ApiReturnObject("200", "操作成功", "操作成功");
        }
        throw new BusinessException(CommonErrorCode.E_200108);
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public ApiReturnObject update(@RequestBody LessonMenu lessonMenu) {
        int i = lessonMenuService.updateLessonMenu(lessonMenu);
        if (i > 0){
            return new ApiReturnObject("200", "操作成功", "操作成功");
        }
        throw new BusinessException(CommonErrorCode.E_200109);
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public ApiReturnObject delete(Integer id) {
        int i = lessonMenuService.deleteLessonMenuById(id);
        if (i > 0){
            return new ApiReturnObject("200", "操作成功", "操作成功");
        }
        throw new BusinessException(CommonErrorCode.E_200110);
    }
}
